/**
 * Copyright (C) 2010 Hamburg Sud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hamburgsud.log4testing.core.data;

import java.util.ArrayList;

/**
 * Class which represents a TestSuite. A TestSuite could contain TestCases
 * and/or other TestSuites
 * <p>
 * 
 * </p>
 * A TestSuite encapsulates a bunch of TestCases and/or TestSuites
 * 
 * @author
 * 
 */
public class TestSuite extends TestStepContainer {

	/**
	 * testCases contains all TestCases of the TestSuite
	 */
	private ArrayList<TestCase> testCases = new ArrayList<TestCase>();
	/**
	 * testSuites contains all testSuites of the TestSuite
	 */
	private ArrayList<TestSuite> testSuites = new ArrayList<TestSuite>();
	/**
	 * containers contains all TestSuites and TestCases
	 */
	private ArrayList<TestStepContainer> containers = new ArrayList<TestStepContainer>(
			testCases.size() + testSuites.size());

	/**
	 * Help-TestSuite to avoid illegal adding of TestSuites
	 */
	private TestSuite parent;

	/**
	 * Constructor which just calls the Super Constructor
	 * 
	 * @param name
	 *            the name of the TestSuite
	 */
	public TestSuite(String name) {
		super(name);
	}

	/**
	 * Adds a TestCase to testCases and to containers
	 * 
	 * @param testCase
	 */
	public void addTestCase(final TestCase testCase) {
		if (testCase != null) {
			testCases.add(testCase);
			containers.add(testCase);
		}
	}

	/**
	 * Adds a TestSuite to the testSuites and to the containers
	 * @param testSuite the TestSuite to add
	 */
	public void addTestSuite(final TestSuite testSuite) {
		if ((testSuite != null)) {
			if (checkParent(testSuite)) {
				testSuite.parent = this;
				testSuites.add(testSuite);
				containers.add(testSuite);
			} else
				throw new IllegalParentException();
		}
	}
	
	/**
	 * Avoids that a TestSuite could add itself and avoids that a circle between
	 * some TestSuites can be created.
	 * @param testSuite
	 * @return
	 */
	public boolean checkParent(final TestSuite testSuite) {
		boolean check = true;
		if (parent != null) {
			if (testSuite.getName().equals(parent.getName())
					|| testSuite.getName().equals(getName()))
				check = false;

			check = check && parent.checkParent(testSuite);
		} else {
			if (testSuite.getName().equals(getName()))
				check = false;
		}

		return check;
	}

	/**
	 * Returns the number of Failed TestSteps in containers
	 */
	@Override
	public int getNumberOfFailed() {
		int numberOfErrors = getNumberOfFailed(containers);
		return numberOfErrors;
	}

	/**
	 * Returns the number of TestSteps in containers
	 */
	@Override
	public int getNumberOfTestSteps() {
		int numberOfTestSteps = getNumberOfTestSteps(containers);
		return numberOfTestSteps;
	}

	/**
	 * Returns the first Failed TestStep in containers
	 */
	@Override
	public TestStep getFirstFailed() {
		final TestStep firstFailed = getFirstFailed(containers);
		return firstFailed;
	}

	/**
	 * @return  the ArrayList of testCases as Iterable
	 */
	public Iterable<TestCase> getTestCases() {
		return testCases;
	}

	/**
	 * @return the ArrayList of TestSuites as Iterable
	 */
	public Iterable<TestSuite> getTestSuites() {
		return testSuites;
	}

	
	/**
	 * returns the first Teststep of the TestSuite
	 */
	@Override
	public TestStep getFirstTestStep() {
		final TestStep firstTestStep = getFirstTestStep(containers);
		return firstTestStep;
	}

	/**
	 * Returns the last Teststep of the TestSuite
	 */
	@Override
	public TestStep getLastTestStep() {
		final TestStep lastTestStep = getLastTestStep(containers);
		return lastTestStep;
	}

	@Override
	public void clear() {
		containers.clear();
		testCases.clear();
		testSuites.clear();
	}

}
